/**
 * Copyright(c) 2015 Jade Techonologies Co., Ltd.
 *
 * History:
 *   15-1-16 下午5:04 Created by lyyang
 */
package com.jade.framework.security.shiro.realm;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 永远认证成功的realm．just for test
 *
 * @author <a href="mailto:lyyang@jade.com">lyyang</a>
 * @version 1.0 15-1-16 下午5:04
 */
public class SayTrueRealm  extends AuthorizingRealm
{
    protected Log logger = LogFactory.getLog(this.getClass());


    public SayTrueRealm()
    {
        setAuthenticationTokenClass(UsernamePasswordToken.class);
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)
    {
        logger.info("=========1233=" + principalCollection.getPrimaryPrincipal());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        List<String> permissions = new ArrayList<String>();
        permissions.add("hello");
        permissions.add("world");
        List<String> roles = new ArrayList<String>();
        roles.add("role1");
        roles.add("role2");
        info.addStringPermissions(permissions);
        info.addRoles(roles);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
            throws AuthenticationException
    {

        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        String password = new String(token.getPassword());
        return new SimpleAuthenticationInfo(username, password,
                getName());

    }

    public boolean hasRole(PrincipalCollection principalCollection, String role)
    {
        logger.info("=========1233=" + principalCollection.getPrimaryPrincipal() + ",role:" + role);
        return super.hasRole(principalCollection, role);
    }

    public boolean isPermitted(PrincipalCollection principalCollection, String permission)
    {
        logger.info("=========1233=" + principalCollection.getPrimaryPrincipal() + ",permisssion:" + permission);
        return super.isPermitted(principalCollection, permission);
    }
}
